package springobjects;

import java.awt.Color;
import java.awt.Graphics2D;
import massobjects.BaseMass;


/**
 * Abstract connector class that all connector objects must extend.
 * 
 * @author David Howdyshell, Zack Hopping
 */
public abstract class BaseConnector {
    // state
    private BaseMass myStart;
    private BaseMass myEnd;
    private double myLength;

    /**
     * Constructor for this class.
     * 
     * @param start - mass connected at start point
     * @param end - mass connected at end point
     * @param length - rest length of the connector
     */
    public BaseConnector (BaseMass start, BaseMass end, double length) {
        myStart = start;
        myEnd = end;
        myLength = length;
    }

    /**
     * Draws a line between start and end point.
     * 
     * @param pen - Graphics2D object for drawing.
     */
    public void paint (Graphics2D pen) {
        int xStart = (int) myStart.getCenter().getX();
        int yStart = (int) myStart.getCenter().getY();
        int xEnd = (int) myEnd.getCenter().getX();
        int yEnd = (int) myEnd.getCenter().getY();
        double dx = xStart - xEnd;
        double dy = yStart - yEnd;
        double len = Math.sqrt(dx * dx + dy * dy) - myLength;

        if (Math.abs(len) < 0.001) {
            pen.setColor(Color.GREEN);
        }
        else if (len < 0.0) {
            pen.setColor(Color.BLUE);
        }
        else {
            pen.setColor(Color.RED);
        }
        pen.drawLine(xStart, yStart, xEnd, yEnd);
    }

    /**
     * Update method must be implemented by child class. Will be different based
     * on connector type.
     * 
     * @param dt - delta time since last update
     */
    public abstract void update (double dt);

    /**
     * Gets pointer to mass at start point.
     * 
     * @return myStart
     */
    public BaseMass getStart () {
        return myStart;
    }

    /**
     * Gets pointer to mass at end point.
     * 
     * @return myEnd
     */
    public BaseMass getEnd () {
        return myEnd;
    }
}
